package com.example.controller;

import com.example.comm.Result;
import com.example.controller.request.UserPageRequest;
import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    /**
     * 测试
     *
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }

    /**
     * 根据id获取用户
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")//查询
    public Result getById(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        if(exist(user)){
            return Result.error("用户名已存在");
        }
        userService.addUser(user);
        return Result.success(user);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    //删除
    public Result delete(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {

        if (exist(user) == Boolean.FALSE) {
            return Result.error("用户名不存在");
        }

        User user1 = userService.getByUsername(user.getUsername());
        if (user1.getPassword().equals(user.getPassword())) {
            return Result.success();
        } else {
            return Result.error("密码错误");
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (exist(user) == Boolean.TRUE) {
            return Result.error("该用户名已被注册");
        }
        userService.addUser(user);
        return Result.success("注册成功");
    }

    private Boolean exist(User user) {
        String name = user.getUsername();
        User user1 = userService.getByUsername(name);
        if (user1 == null) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @GetMapping("/page")
    public Result page(UserPageRequest userPageRequest) {
        // 指定username模糊匹配
        ExampleMatcher matcher = ExampleMatcher.matching().
                withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase());
        User user = new User();
        user.setUsername(userPageRequest.getUsername());
        Example<User> example = Example.of(user, matcher);
        Pageable pageable = PageRequest.of(userPageRequest.getPageNum()-1, userPageRequest.getPageSize(), Sort.by(Sort.Direction.DESC, "id"));
        Page<User> page = userDao.findAll(example, pageable);
        return Result.success(page);
    }


}
